﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RecruitStudents.Utility;
using System.Data;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using RecruitStudents.Utility.Json;
using System.IO;
using System.Drawing;

namespace RecruitStudents.Client.BLL
{
    class ServiceExaminee
    {
        WS_Examinee.ExamineeService examineeService = new WS_Examinee.ExamineeService();
        WS_Examinee.MySoapHeader objHeader = new WS_Examinee.MySoapHeader();
        internal ServiceExaminee()
        {
            objHeader.Username = "admin";
            objHeader.Password = "xy";
            examineeService.MySoapHeaderValue = objHeader;
        }

        #region 新加的考试分组  上戏统考 
        public void ExecuteNonQuery(string strSql)
        {
            examineeService.ExecuteNonQuery(strSql);
        }
        public DataTable GetTable(string strSql)
        {
            return examineeService.GetTable(strSql).ToDataTable();
        }

        public JsonMessage SaveExamGroup(TB_Examinee_ExamGroup model)
        {
            try
            {
                string strJson = model.ToJson();
                var obj = examineeService.SaveExamGroup(strJson);
                var result = obj.ToList<JsonMessage>();
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion

        #region 上戏考生成绩查询
        public DataSet GetExamineeScoreSearchFormExcel(string strExamPlaceNo, string strSpecNo, string strExamLevel,
            string strName, string strIdCard, string strNMT_SignUpID, string strExamRoomNo, string strExamYear,
            string strStartDate, string strEndDate)
        {
            return examineeService.GetExamineeScoreSearchFormExcel(strExamPlaceNo, strSpecNo, strExamLevel, strName, strIdCard,
                strNMT_SignUpID, strExamRoomNo, strExamYear, strStartDate, strEndDate);
        }
        public DataTable GetExamineeScoreSearchForm1(string strExamPlaceNo, string strSpecNo, string strExamLevel,
            string strName, string strIdCard, string strNMT_SignUpID, string strExamRoomNo, string strExamYear,
            string strStartDate, string strEndDate, string strAllowExamID)
        {
            return examineeService.GetExamineeScoreSearchForm1(strExamPlaceNo, strSpecNo, strExamLevel, strName, strIdCard,
                strNMT_SignUpID, strExamRoomNo, strExamYear, strStartDate, strEndDate, strAllowExamID).ToDataTable();
        }
        public DataTable GetExamineeScoreSearchForm2(string strExamPlaceNo, string strSpecNo, string strExamLevel,
            string strName, string strIdCard, string strNMT_SignUpID, string strExamRoomNo, string strExamYear,
            string strStartDate, string strEndDate, string strAllowExamID)
        {
            return examineeService.GetExamineeScoreSearchForm2(strExamPlaceNo, strSpecNo, strExamLevel, strName, strIdCard,
                strNMT_SignUpID, strExamRoomNo, strExamYear, strStartDate, strEndDate, strAllowExamID).ToDataTable();
        }
        public DataTable GetExamineeScoreSearchForm3(string strExamPlaceNo, string strSpecNo, string strExamLevel,
            string strName, string strIdCard, string strNMT_SignUpID, string strExamRoomNo, string strExamYear,
            string strStartDate, string strEndDate, string strAllowExamID)
        {
            return examineeService.GetExamineeScoreSearchForm3(strExamPlaceNo, strSpecNo, strExamLevel, strName, strIdCard,
                strNMT_SignUpID, strExamRoomNo, strExamYear, strStartDate, strEndDate, strAllowExamID).ToDataTable();
        }


        #endregion

        public DataTable GetSignUpStudentsByExcel_New(string strExamRoomNo)
        {
            return examineeService.GetSignUpStudentsByExcel_New(strExamRoomNo).ToDataTable();
        }
        public DataTable GetSpecialityExameeSearchDataTable(string strExamPlaceNo, string strSpecNo, string strExamLevel,
            string strName, string strIdCard, string strNMT_SignUpID, string strExamRoomNo, string strExamYear)
        {
            return examineeService.GetSpecialityExameeSearchDataTable(strExamPlaceNo, strSpecNo, strExamLevel, strName, strIdCard, strNMT_SignUpID, strExamRoomNo, strExamYear).ToDataTable();
        }
        public DataTable GetExameeSpeciality_ExamLevelBySignUpID_New(string strSignUpID)
        {
            return examineeService.GetExameeSpeciality_ExamLevelBySignUpID_New(strSignUpID).ToDataTable();
        }

        #region 根据高考报名号获取生源地
        public DataTable Check_SYD_By_YKZ(string strYKZ)
        {
            return examineeService.Check_SYD_By_YKZ(strYKZ).ToDataTable();
        }
        #endregion

        #region  根据考生号查询，判断考生号是否已经存在
        public DataTable GetStudentsInfoByYKZ(string strNMT_SignUpID)
        {
            return examineeService.GetStudentsInfoByYKZ(strNMT_SignUpID).ToDataTable();
        }
        #endregion

        #region 根据考生号长度查询，获取生源地映射信息
        public DataTable GetNativePlaceNOByLen(string strNMT_SignUp_Len)
        {
            return examineeService.GetNativePlaceNOByLen(strNMT_SignUp_Len).ToDataTable();
        }
        #endregion

        #region 根据考生匹配内容获取生源地信息
        public DataTable GetNativePlaceNOByMask(string strMask)
        {
            return examineeService.GetNativePlaceNOByMask(strMask).ToDataTable();
        }
        #endregion

        #region  根据考点编号获取受限制的考点
        public DataTable GetExamPlace_Limit(string strExamPlaceNO)
        {
            return examineeService.GetExamPlace_Limit(strExamPlaceNO).ToDataTable();
        }
        #endregion

        #region 获取 考生基本信息(根据考生ID) 
        public TB_SignUpStudents GetExameeByID(string strSignUpID)
        {
            return examineeService.GetExameeByID(strSignUpID).ToList<TB_SignUpStudents>();
        }
        #endregion

        #region 获取 考生基本信息(根据高考报名号) 
        public DataTable GetExameeByNMT_SignUpID(string strNMT_SignUpID)
        {
            return examineeService.GetExameeByNMT_SignUpID(strNMT_SignUpID).ToDataTable();
        }
        #endregion

        #region 获取 考生基本信息(根据身份证号) 
        public DataTable GetExameeByIdentityCard(string strIdentityCard)
        {
            return examineeService.GetExameeByIdentityCard(strIdentityCard).ToDataTable();
        }
        #endregion

        #region 获取 考生缴费信息（找出相应专业的缴费情况） 
        public DataTable GetExameeNetPayFeeDataTable_New(string strIdentityCard)
        {
            return examineeService.GetExameeNetPayFeeDataTable_New(strIdentityCard).ToDataTable();
        }

        #endregion

        #region 获取 考生报考专业 财务订单付款信息(根据考生ID) 
        public DataTable GetExameeSpeciality_NetPayFeeInfo_BySignUpID(string strSignUpID)
        {
            return examineeService.GetExameeSpeciality_NetPayFeeInfo_BySignUpID(strSignUpID).ToDataTable();
        }
        #endregion

        #region 获取 财务订单付款信息(根据考生ID)
        public DataTable GetNetPayFeeInfo_BySignUpID(string strSignUpID)
        {
            return examineeService.GetNetPayFeeInfo_BySignUpID(strSignUpID).ToDataTable();
        }
        #endregion

        #region 获取 财务订单明细(根据订单号)
        public DataTable GetNetPayFeeDetail_ByOrdNO(string strOrdNO)
        {
            return examineeService.GetNetPayFeeDetail_ByOrdNO(strOrdNO).ToDataTable();
        }
        #endregion

        #region 获取 考生报考专业,考试级别(根据考生ID)
        public DataTable GetExameeSpeciality_ExamLevelBySignUpID(string strSignUpID)
        {
            return examineeService.GetExameeSpeciality_ExamLevelBySignUpID(strSignUpID).ToDataTable();
        }
        #endregion

        #region 获取 考生报考专业(根据考生ID)
        public DataTable GetExameeSpecialityBySignUpID(string strSignUpID)
        {
            return examineeService.GetExameeSpecialityBySignUpID(strSignUpID).ToDataTable();
        }
        #endregion

        #region  根据考生ID删除报考专业信息
        public bool DeleteExamSignUpStudentsSpecialityBySignUpID(string strSignUpID, string strIsFee)
        {
            try
            {
                DevHelper.ShowWaiting();
                var obj = examineeService.DeleteExamSignUpStudentsSpecialityBySignUpID(strSignUpID, strIsFee);
                var result = obj.ToList<JsonMessage>();
                DevHelper.HideWaiting();
                if (result.State.ToLower() == "false")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                DevHelper.HideWaiting();
                return false;
            }
        }
        #endregion

        #region 获取 考点、专业、分组关联信息
        public DataTable GetSpecialityKindsDataTable(string strExamPlaceNO)
        {
            return examineeService.GetSpecialityKindsDataTable(strExamPlaceNO).ToDataTable();
        }
        #endregion

        #region 获取考生照片
        public DataSet GetStudentsPhoto(string strSignUpStudentsID)
        {
            return examineeService.GetStudentsPhoto(strSignUpStudentsID);
        }
        #endregion

        #region 保存考生基本信息
        public bool SaveStudentInfo_Modify(TB_SignUpStudents model)
        {
            try
            {
                string strJson = model.ToJson();
                DevHelper.ShowWaiting();
                var obj = examineeService.SaveStudentInfo_Modify(strJson);
                var result = obj.ToList<JsonMessage>();
                DevHelper.HideWaiting();
                if (result.State.ToLower() == "false")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                DevHelper.HideWaiting();
                return false;
            }
        }
        #endregion

        #region 保存 考生信息（学生基本信息 咨询互动用户表  报考专业信息  财务订单付款  财务订单明细 打印表）
        public bool SaveSignUpStudentsInfo(string strJson)
        {
            try
            {
                DevHelper.ShowWaiting();
                var obj = examineeService.SaveSignUpStudentsInfo(strJson);
                var result = obj.ToList<JsonMessage>();
                DevHelper.HideWaiting();
                if (result.State.ToLower() == "false")
                {
                    //DevHelper.ShowErrorMessageBox(result.Msg);
                    return false;
                }
                else
                {
                    //DevHelper.ShowInfoMessageBox(result.Msg);
                    return true;
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                DevHelper.HideWaiting();
                return false;
            }
        }
        #endregion

        #region  保存专业补报信息（报考专业信息  财务订单付款  财务订单明细
        public bool SavaSpecialityPackInfo(string strJson)
        {
            try
            {
                DevHelper.ShowWaiting();
                var obj = examineeService.SavaSpecialityPackInfo(strJson);
                var result = obj.ToList<JsonMessage>();
                DevHelper.HideWaiting();
                if (result.State.ToLower() == "false")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                DevHelper.HideWaiting();
                return false;
            }
        }
        #endregion

        #region 保存缴费确认信息（更新报考专业信息  保存财务订单付款  保存财务订单明细）
        public bool UpdateSpecialityInfoState(string strJson)
        {
            try
            {
                DevHelper.ShowWaiting();
                var result = examineeService.UpdateSpecialityInfoState(strJson).ToList<JsonMessage>();
                DevHelper.HideWaiting();
                if (result.State.ToLower() == "false")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                DevHelper.HideWaiting();
                return false;
            }
        }
        #endregion

        #region 上传照片
        public bool UploadPhoto(string strSignUpID, string strPicData)
        {
            try
            {
                DevHelper.ShowWaiting();
                var result = examineeService.UploadPhoto(strSignUpID, strPicData).ToList<JsonMessage>();
                DevHelper.HideWaiting();
                if (result.State.ToLower() == "false")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                DevHelper.HideWaiting();
                return false;
            }
        }
        #endregion

        #region 打印准考证
        public bool UpdatePrintAllowExamIDData_All(string strSignUpID, string strComputerName)
        {
            try
            {
                examineeService.UpdatePrintAllowExamIDData_All(strSignUpID, strComputerName);
                return true;
            }
            catch (Exception ex)
            {
                DevHelper.ShowErrorMessageBox("操作数据出现错误:" + ex.Message);
                return false;
            }
        }
        #endregion

        #region 获取考试信息导出excel  
        public DataTable GetSignUpStudentsByExcel(string strExamPlaceID, string strExamLevel, string strSpecialityKindsNO)
        {
            return examineeService.GetSignUpStudentsByExcel(strExamPlaceID, strExamLevel, strSpecialityKindsNO).ToDataTable();
        }
        #endregion

        #region 获取 考生相关信息（专业信息查询）
        public DataTable GetExamineeOfAllInfo(string strExamPlaceID, string strSpecialityKindsNO, string strAppointmentGroup, string strPaySource, string strIsPass,
            string strIsDownload, string strExamLevel, string strName, string strNMT_SignUpID, string strIdCard, string strAllowExamID,
            string strCurrentIndex, string strPageSize, out int totalCount, out decimal feeMoney)
        {
            totalCount = 0;
            feeMoney = 0;
            DevHelper.ShowWaiting();
            var result = examineeService.GetExamineeOfAllInfo(strExamPlaceID, strSpecialityKindsNO, strAppointmentGroup, strPaySource, strIsPass,
              strIsDownload, strExamLevel, strName, strNMT_SignUpID, strIdCard, strAllowExamID,
              strCurrentIndex, strPageSize).ToList<PagerCondition>();
            if (result != null)
            {
                var dt = result.DataJson.ToDataTable();
                totalCount = result.RecordCount;
                feeMoney = result.OtherData.ToSafeDecimal();
                DevHelper.HideWaiting();
                return dt;
            }
            return null;
        }
        #endregion

        #region 获取 考生相关信息（考生信息查询）
        public DataTable GetExamineeOfAllInfo_ByBaseInfo(string strName, string strNMT_SignUpID, string strIdCard, string strCurrentIndex,
            string strPageSize, out int totalCount, out decimal feeMoney, string strExamYear)
        {
            totalCount = 0;
            feeMoney = 0;
            var result = examineeService.GetExamineeOfAllInfo_ByBaseInfo(strName, strNMT_SignUpID,
                strIdCard, strCurrentIndex, strPageSize, strExamYear).ToList<PagerCondition>();
            if (result != null)
            {
                var dt = result.DataJson.ToDataTable();
                totalCount = result.RecordCount;
                feeMoney = result.OtherData.ToSafeDecimal();
                return dt;
            }
            return null;
        }
        #endregion

        internal string GetExamTime(string strExamPlaceNo, string strSpecialityKindsNO)
        {
            return examineeService.GetExamTime(strExamPlaceNo, strSpecialityKindsNO);
        }

        internal string GetExamLog(string strSignUpID, string strSpecialityKindsNO)
        {
            return examineeService.GetExamLog(strSignUpID, strSpecialityKindsNO);
        }

        internal bool SaveStudentRegistration(TB_SignUpStudents model, TB_ConsultUsers consultUsersModel, TB_Exam_SignUpStudentsSpeciality examModel)
        {
            string JsonSignUpStudents = string.Empty;
            string JsonConsultUsers = string.Empty;
            string JsonExamSignUpStudentsSpeciality = string.Empty;
            if (model != null)
            {
                JsonSignUpStudents = model.ToJson();
            }
            if (consultUsersModel != null)
            {
                JsonConsultUsers = consultUsersModel.ToJson();
            }
            if (examModel != null)
            {
                JsonExamSignUpStudentsSpeciality = examModel.ToJson();
            }
            JsonMessage message = examineeService.SaveStudentRegistration(JsonSignUpStudents, JsonConsultUsers, JsonExamSignUpStudentsSpeciality).ToList<JsonMessage>();
            if (message.State.ToLower() == "true")
            {
                return true;
            }
            else
            {
                DevHelper.ShowErrorMessageBox(message.Msg);
                return false;
            }
        }

        internal bool ComplateStudentRegistration(TB_SignUpStudents model, List<TB_Exam_SignUpStudentsSpeciality> listSpeciality)
        {
            string JsonSignUpStudents = string.Empty;
            string JsonExamSignUpStudentsSpeciality = string.Empty;
            if (model != null)
            {
                JsonSignUpStudents = model.ToJson();
            }
            if (listSpeciality != null)
            {
                JsonExamSignUpStudentsSpeciality = listSpeciality.ToJson();
            }
            JsonMessage message = examineeService.ComplateStudentRegistration(JsonSignUpStudents, JsonExamSignUpStudentsSpeciality, SessionerHelper.UserID).ToList<JsonMessage>();
            if (message.State.ToLower() == "true")
            {
                return true;
            }
            else
            {
                DevHelper.ShowErrorMessageBox(message.Msg);
                return false;
            }
        }

        internal bool UpdateSignUpStudentsSpeciality_TZ(string strSignUpStudentsSpecialityID, string strUpdateMessage)
        {
            JsonMessage message = examineeService.UpdateSignUpStudentsSpeciality_TZ(strSignUpStudentsSpecialityID, strUpdateMessage, SessionerHelper.UserID).ToList<JsonMessage>();
            if (message.State.ToLower() == "true")
            {
                return true;
            }
            else
            {
                DevHelper.ShowErrorMessageBox(message.Msg);
                return false;
            }
        }

        internal bool UpdateSignUpStudentsSpeciality(string strSignUpStudentsSpecialityID, string strExamPlaceNo, string strSpecNo,
            string strUpdateMessage)
        {
            JsonMessage message = examineeService.UpdateSignUpStudentsSpeciality(strSignUpStudentsSpecialityID, strExamPlaceNo, strSpecNo, strUpdateMessage, SessionerHelper.UserID).ToList<JsonMessage>();
            if (message.State.ToLower() == "true")
            {
                return true;
            }
            else
            {
                DevHelper.ShowErrorMessageBox(message.Msg);
                return false;
            }
        }

        internal DataTable GetExamSigned(string strSignUpStudentsSpecialityID)
        {
            return examineeService.GetExamSigned(strSignUpStudentsSpecialityID).ToDataTable();
        }



        /// <summary>
        /// 获取需要导入的照片信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        internal bool Getphoto(string path)
        {
            DirectoryInfo d = new DirectoryInfo(path);
            ImageConverter imgCvt = null;
            TB_SignUpStudentsPhoto model = null;
            byte[] theData = null;
            Image myimage = null;
            foreach (FileInfo f in d.GetFiles("*.jpg"))
            {
                theData = null;
                model = null;
                imgCvt = new ImageConverter();
                // 获取文件名，文件后缀名和文件大小----------------------------
                string b = f.ToString().Trim();
                string strfilename = b.Substring(0, b.LastIndexOf("."));
                string strsuffix = f.ToString().Substring(b.LastIndexOf(".") + 1).Trim();
                string strsize = f.Length.ToString().Trim();
                // 获取照片转化为byte------------------------------------------
                string aa = path.Trim() + "\\" + f.ToString().Trim();
                myimage = Image.FromFile(aa.ToString().Trim());
                theData = (byte[])imgCvt.ConvertTo(myimage, typeof(byte[]));
                model = new TB_SignUpStudentsPhoto();
                model.Photo = theData;
                model.PhotoName = strfilename + ".jpg";
                model.PhotoSize = strsize;
                model.PhotoSuffix = "jpg";
                model.SignUpStudentsID = strfilename;
                try
                {
                    UploadPhoto(model);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        private void UploadPhoto(TB_SignUpStudentsPhoto model)
        {
            string strJson = JsonHelper.JsonSerializer<TB_SignUpStudentsPhoto>(model);
            examineeService.ImportUploadPhoto(strJson);
        }

        internal JsonMessage ImportSignUpStudents(string strSignUpStudents, string strSignUpStudentsExamSpec)
        {
            return examineeService.ImportSignUpStudents(strSignUpStudents, strSignUpStudentsExamSpec).ToList<JsonMessage>();
        }
    }
}
